/* eslint-disable no-param-reassign */

import sysAnnounceApi from '@/api/modules/system/announce'
const initQueryParams = {
  // 公告名称
  name: undefined,
  // 公告类型
  type: undefined,
  // 发布渠道
  pushChannel: undefined,
  //用户  
  userName:undefined,
  //角色  
  roleName:undefined,
  // 公告列表状态
  listStatus: undefined,
}
const state = {
  // 列表查询条件
  queryParams: {
    ...initQueryParams
  },
  UN_READ: 0, //未读消息数
  UN_READ_LIST: [], //未读消息体
  order: undefined,
  UPDATE_NEWS:'',
  IS_POP:false,
  POP_DATA:{},
}
const mutations = {
  resetSysAnnounceQueryParams (state) {
    state.queryParams = {
      ...initQueryParams
    }
  },
  setOrder (state, value) {
    state.order = value
  },
  SET_UN_READ: (state, data) => {
    state.UN_READ = data
  },
  SET_UN_READ_LIST: (state, data) => {
      state.UN_READ_LIST = data
  },
  SET_UPDATE_NEWS: (state, data) => {
      state.UPDATE_NEWS = data
  },
  SET_IS_POP:(state, data) => {
    state.IS_POP = data
  },
  SET_POP_DATA:(state, data) => {
    state.POP_DATA = data
  }
}
const actions = {
  queryUnRead({ commit }) {
    return new Promise((resolve,reject)=>{
      sysAnnounceApi.getSysAnnounceList().then(res => {
          // if (res.data) {
        commit('SET_UN_READ', res.data[0].unreadCount)
        commit('SET_UN_READ_LIST',res.data)
        resolve(true)
          // }
      }).catch(err=>{
        reject(err)
      })
    })
  },
  queryUpdateNews({ commit }) {
    return new Promise((resolve,reject)=>{
      sysAnnounceApi.getSysUpdateNews().then(res => {
        if (!!res.data) {
          commit('SET_UPDATE_NEWS',res.data)
        }
        resolve(true)
      }).catch(err=>{
        reject(err)
      })
    })
  },
  queryIsPop({ commit }) {
    return new Promise((resolve,reject)=>{
      sysAnnounceApi.isAlertPopup().then(res => {
          // if (res.data) {
        commit('SET_IS_POP',res.data.popupTag)
        commit('SET_POP_DATA',res.data)
        resolve(true)
          // }
      }).catch(err=>{
        reject(err)
      })
    })
  },
}

export default {
  state,
  mutations,
  actions
}
